<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use App\Http\model\Car;
use App\Http\Comm\CommonController as common;

class CarController extends Controller
{
    /**
     * [upload 图片上传]
     * @Author   孙丽男
     * @DateTime 2018-06-20
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function upload(Request $request){

        if (!$request->hasFile('logo')) {

            $result['code'] = 1002;
            $result['msg'] = '上传文件为空';
            return json_encode($result);
        }

        //获取UploadFile实例
        $file = $request->file('logo'); 
        
        $path = "uploads/". date("Ym")."/";

        $common = new common;
        //上传图片
        $upload = $common->upload($file,$path);

        if (!$upload) {

            $result['code'] = 1008;
            $result['msg'] = '上传失败';
            return json_encode($result);
        }
        
        $result['code'] = 1000;
        $result['msg'] = '上传成功';
        $result['pic'] = $upload;
        return json_encode($result);
    }
	/**
	 * [addBrand 添加品牌]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [品牌名称] str $name
	 * @param    [子品牌名称] array $childName[]
	 * @param    [品牌logo] str  $logo
	 * @version  6.2
	 */
	public function addBrand(Request $request){

		$result = array();

    	if ($request['name'] && $request['childName'] && $request['logo']) {
    		
		 	$child = $request['childName'];
		 	$brand['name'] = $request['name'] ;
            $brand['logo'] = $request['logo'] ;

		 	$car = new Car;

		 	$result = $car->addBrandModel($brand,$child);

		 	return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}
	}

	/**
	 * [findBrand 品牌列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [开始时间] date $start_time  可选
	 * @param    [结束时间] date $end_time  可选
	 * @version  6.2
	 */
	public function findBrand(Request $request){

		//构建查询条件
        $where = array();

         if ($request['brandId']) {
            
            $where = [
                ['car_brand.id', $request['brandId']],
            ]; 
        }

        if ($request['name']) {
            
            $where[1] = ['car_brand.name', 'like', '%' . $request['name'] . '%'];
        }

		if ($request['start_time']) {
            
            $where[2] =  ['car_brand.modtime', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['car_brand.modtime', '<',  $request['end_time']];
        }

        $where[4] = ['car_brand.pid', '=',  0];

        $where[5] =  ['car_brand.isdel', 1 ];

        $car = new Car;

        $result = $car->findBrandModel($where);

        return $result;
	}

	/**
	 * [deleteBrand 删除品牌，软删除]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌id] arr $brandId[]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteBrand(Request $request){

		 $result = array();
   
        if (is_array($request['brandId'])) {

            $brandIdArr = $request->get('brandId');

            $car = new Car;

            $result = $car->deleteBrandModel($brandIdArr);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
	}

	/**
	 * [deleteBrand 删除子品牌，软删除]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌id] int $brandId
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteChild(Request $request){

		 $result = array();
   
        if ($request['brandId']) {

            $car = new Car;

            $result = $car->deleteChildModel($request['brandId']);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
	}

	/**
	 * [updateBrand 修改品牌]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [修改数据] 二维数组 $data
	 * @param    [图片] logo
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateBrand(Request $request){

		$result = array();

	    $data = $request->input('data',0);

    	if ($data) {

		 	$addData = array();
		 	foreach ($data as $key => $value) {
                if (!is_array($value)) {
                    $result['code'] = 1003;
                    $result['msg'] = '参数格式错误';
                    return json_encode($result);
                }

                $data[$key]['logo'] = $request['logo'];
    			//判断是添加还是修改子品牌
    			if (empty($value['brandId'])) {
    				
    				$addData[] = $data[$key];

    				unset($data[$key]);
    			}
                
    		}

		 	$car = new Car;

		 	$result = $car->updateBrandModel($data,$addData);

		 	return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}
	}
	/*
    |--------------------------------------------------------------------------
    | 操作车系部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addSeries 添加车系]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [子品牌品牌id] int $brandid
    * @version  6.2
    */
    public function addSeries(Request $request){
    	$result = array();

    	if ($request['name'] && $request['brandid'] && $request['pic']) {
    		
		 	$series['name'] = $request['name'] ;
		 	$series['brandid'] = $request['brandid'] ;
		 	$series['pic'] = $request['pic'];

		 	$car = new Car;

		 	$result = $car->addSeriesModel($series);

		 	return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}

    }

    /**
     * [findSerise 数据列表]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function findSeries(Request $request){

    	//构建查询条件
        $where = array();

         if ($request['seriesId']) {
            
            $where = [
                ['car_series.id', $request['seriesId']],
            ]; 
        }

        if ($request['name']) {
            
            $where[1] = ['car_series.name', 'like', '%' . $request['name'] . '%'];
        }

		if ($request['start_time']) {
            
            $where[2] =  ['car_series.modtime', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['car_series.modtime', '<',  $request['end_time']];
        }
        $where[4] =  ['car_series.isdel', 1 ];
        $car = new Car;

        $result = $car->findSeriesModel($where);

        return $result;
    }

    /**
     * [deleteSeries 删除车系，软删除]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [车系id] arr $seriesId[]
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function deleteSeries(Request $request){

    	$result = array();
   
        if (is_array($request['seriesId'])) {

            $seriesIdArr = $request->get('seriesId');

            $car = new Car;

            $result = $car->deleteSeriesModel($seriesIdArr);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [updateSeries 修改车系]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function updateSeries(Request $request){

    	$result = array();

    	if ($request['name'] && $request['brandid'] && $request['id'] && $request['pic']) {
    		
		 	$data['name'] = $request['name'] ;
		 	$data['brandid'] = $request['brandid'] ;
		 	$seriesId = $request['id'] ;
		 	$data['pic'] = $request['pic'];

		 	$car = new Car;

		 	$result = $car->updateSeriesModel($data,$seriesId);

		 	return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}
    }
    /*
    |--------------------------------------------------------------------------
    | 操作级别部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addLevel 添加级别]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [type]
    * @version  6.2
    */
   public function addLevel(Request $request){

   		$result = array();

    	if ($request['name'] && $request['pid'] || $request['pid']=='0'  && $request['order']) {
    		
		 	$data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }

		 	$car = new Car;

		 	$result = $car->addLevelModel($data);

		 	return $result;

    	}else{

    		$result['code'] = 1002;
    		$result['msg'] = '参数为空';
    		return json_encode($result);

    	}

   }

   /**
    * [findLevel 级别列表]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @version  6.2
    * @return   [type]     [description]
    */
   public function findLevel(Request $request){

   		//构建查询条件
        $where = array();

         if ($request['levelId']) {
            
            $where = [
                ['car_level.id', $request['levelId']],
            ]; 
        }

        if ($request['name']) {
            
            $where[1] = ['car_level.name', 'like', '%' . $request['name'] . '%'];
        }

		if ($request['start_time']) {
            
            $where[2] =  ['car_level.modtime', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['car_level.modtime', '<',  $request['end_time']];
        }
        $where[4] =  ['car_series.isdel', 1 ];
        $car = new Car;

        $result = $car->findLevelModel($where);

        return $result;
   }

   /**
    * [setStatus 删除/冻结]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [级别id] arr $levelId[]
    * @version  6.2
    */
    public function setStatus(Request $request){

    	$result = array();

        if (is_array($request['levelId'])) {

            $levelIdArr = $request->get('levelId');

            if ($request['status']) {
        		
        		$update['status'] = $request['status'];
        	}else{
        		$update['isdel'] = 2; 
        	}

            $car = new Car;

            $result = $car->setStatusModel($levelIdArr,$update);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }

    /**
     * [updateLevel 修改级别]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [type]
     * @version  6.2
     * @return   [type]     [description]
     */
    public function updateLevel(Request $request){
    	$result = array();
   
        if ($request['levelId'] && $request['name'] &&  $request['pid'] || $request['pid']=='0' && $request['level']&&$request['order']) {

        	$data = $request->all();
             if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }

            unset($data['levelId']);
            $car = new Car;

            $result = $car->updateLevelModel($data,$request['levelId']);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
    }
    /*
    |--------------------------------------------------------------------------
    | 车型部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addType 车型添加]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @version  6.2
    */
   public function addType(Request $request){

   		$result = array();

   		if ($request['brandid'] && $request['sid'] && $request['name'] && $request['level'] && $request['type'] && $request['listtime'] &&  $request['displacement'] &&  $request['gearbox'] &&  $request['energy'] &&  $request['structure'] &&  $request['maxride'] &&  $request['door'] &&  $request['seating'] &&  $request['fuel'] && $request['enpro'] && $request['miscellaneous'] && $request['pic']) {
   			
   			$data = $request->all();
             if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }

   			$data['pic'] = json_encode($data['pic']);

   			 $car = new Car;

            $result = $car->addTypeModel($data);

            return $result;
   		}else{

   			$result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
   		}
   }

   /**
    * [findType 车型列表]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [type]
    * @version  6.2
    * @return   [type]     [description]
    */
   public function findType(Request $request){
   		//构建查询条件
        $where = array();

         if ($request['typeId']) {
            
            $where = [
                ['car_type.id', $request['typeId']],
            ]; 
        }

        if ($request['name']) {
            
            $where[1] = ['car_type.name', 'like', '%' . $request['name'] . '%'];
        }

		if ($request['start_time']) {
            
            $where[2] =  ['car_type.modtime', '>',  $request['start_time']];
        }

        if ($request['end_time']) {
            
            $where[3] =  ['car_type.modtime', '<',  $request['end_time']];
        }
        $where[4] =  ['car_type.isshow', 1 ];
        $where[5] =  ['car_type.isdel', 1 ];
        $car = new Car;

        $result = $car->findTypeModel($where);

        return $result;
   }

   /**
	 * [deleteType 删除车型，软删除/设置展示或不展示]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [品牌id] arr $typeId[]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteType(Request $request){

		 $result = array();
   
        if (is_array($request['typeId'])) {

            $typeIdArr = $request->get('typeId');

            if ($request['isshow']) {
        		
        		$update['isshow'] = $request['isshow'];
        	}else{
        		$update['isdel'] = 2; 
        	}

            $car = new Car;

            $result = $car->deleteTypeModel($typeIdArr,$update);

            return $result;

        }else{

            $result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
        }
	}

	/**
	 * [updateType 修改车型]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateType(Request $request){

		$result = array();

   		if ($request['brandid'] && $request['sid'] && $request['name'] && $request['level'] && $request['type'] && $request['listtime'] &&  $request['displacement'] &&  $request['gearbox'] &&  $request['energy'] &&  $request['structure'] &&  $request['maxride'] &&  $request['door'] &&  $request['seating'] &&  $request['fuel'] && $request['enpro'] && $request['miscellaneous'] && $request['typeId'] && $request['pic']) {

   			$data = $request->all();
             if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }

   			$data['pic'] = json_encode($data['pic']);

            $typeId = $request['typeId'];

            unset($data['typeId']);

   			$car = new Car;

            $result = $car->updateTypeModel($data,$typeId);

            return $result;
   		}else{

   			$result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
   		}
	}
	/*
    |--------------------------------------------------------------------------
    | 添加、修改下拉选项数据展示部分
    |--------------------------------------------------------------------------
    */
   /**
    * [brand 品牌]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @version  6.2
    * @return   [type]     [description]
    */
    public function brand(Request $request){
        $pid = '';
   		if ($request['pid']) {
   			
   			$pid = $request['pid'];
   		}

   		$car = new Car;
		$result = $car->brandModel($pid);

		return $result;
    }

    /**
     * [level 级别]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @version  6.2
     * @return   [json]     [$result]
     */
    public function level(){

    	$car = new Car;
		$result = $car->levelModel();

		return $result;
    }
    /**
     * [series 车系]
     * @Author   孙丽男
     * @DateTime 2018-06-07
     * @param    [品牌id] int $brandId
     * @version  6.2
     * @return   [type]     [description]
     */
    public function series(Request $request){

    	$result = array();

   		if ($request['brandId']) {
   			
   			$car = new Car;
			$result = $car->seriesModel($request['brandId']);

			return $result;
   		}else{

   			$result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
   		}
    }

    /**
     * [type 车型列表]
     * @Author   孙丽男
     * @DateTime 2018-06-08
     * @param    [车系id] int $seriesId
     * @version  6.2
     * @return   [type]     [description]
     */
    public function type(Request $request){

    	$result = array();

   		if ($request['seriesId']) {
   			
   			$car = new Car;
			$result = $car->typeModel($request['seriesId']);

			return $result;
   		}else{

   			$result['code'] = 1002;
            $result['msg'] = '参数为空';
            return json_encode($result);
   		}
    }

}